//
//  ViewController.m
//  QQ-Zone
//
//  Created by 张洪毓 on 15/11/24.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

#import "ViewController.h"
#import "UIImage+XL.h"
#import "MainViewController.h"
#import "SVProgressHUD.h"
#define loginError @"用户名或密码错误"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwdTextField;
@property (weak, nonatomic) IBOutlet UIView *myView;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *passwdButton;
@property (weak, nonatomic) IBOutlet UIButton *autoLoginButton;
@property (nonatomic,strong) UIActivityIndicatorView *flower;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *norImage = [UIImage resizableImage:@"login_button_normal"];
    UIImage *pressedImage = [UIImage resizableImage:@"login_button_pressed"];

    [self.loginButton setBackgroundImage:norImage forState:UIControlStateNormal];
    [self.loginButton setBackgroundImage:pressedImage forState:UIControlStateHighlighted];
}

- (IBAction)remDidClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == NO) {
        self.autoLoginButton.selected = NO;
    }
}

- (IBAction)autoLoginDidClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        self.passwdButton.selected = YES;
    }
}

- (IBAction)loginButton:(UIButton *)sender {
    
    [self.flower startAnimating];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if ([self.accountTextField.text isEqualToString:@""]) {
            [self showErrorMsg:@"账号不能为空"];
            return ;
        }
        if ([self.passwdTextField.text isEqualToString:@""]) {
            [self showErrorMsg:@"密码不能为空"];
            return;
        }
        
        if (![self.accountTextField.text isEqualToString:@"123"]) {
            [self showErrorMsg:loginError];
            return ;
        }
        
        if (![self.passwdTextField.text isEqualToString:@"123"]) {
            [self showErrorMsg:loginError];
            return;
        }
        
        MainViewController *main = [[MainViewController alloc] init];
        [self presentViewController:main animated:YES completion:nil];
        
    });
}

-(void)showErrorMsg:(NSString *)msg{
    
    [SVProgressHUD showErrorWithStatus:msg];
    
    CAKeyframeAnimation *ani = [[CAKeyframeAnimation alloc] init];
    ani.duration = 1;
    ani.keyPath = @"transform.translation.x";
    ani.values = @[@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0,@-20,@40,@0];
    [self.myView.layer addAnimation:ani forKey:nil];
    
}



@end
